<?php
$first_row = true;
$fieldnames = array();
if (($handle = fopen("data/data.csv", "r")) !== FALSE) {
  $xml = new SimpleXMLElement("<?xml version=\"1.0\" encoding=\"utf-8\"?><students></students>");
  while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    if ($first_row){
      $fieldnames = $data;
      $first_row = false;
      continue;
    }
    $num = count($data);
    $row++;
    $student = $xml->addChild("student");
    for ($c=0; $c < $num; $c++) {
      $student->addChild($fieldnames[$c], $data[$c]);
    }
  }
  fclose($handle);
  $dom = new DOMDocument('1.0');
  $dom->preserveWhiteSpace = false;
  $dom->formatOutput = true;
  $dom->loadXML($xml->asXML(), LIBXML_NOBLANKS);
  $dom->save("data/student.xml");
}
?>